home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / demostuf / rubber1.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-07-25  |  9.2 KB  |  572 lines

  1. program rubbervector1;
  2. {
  3.     RubberVector #1
  4.     - by Bjarke Viksoe
  5.     16/2/1994
  6.  
  7.   THIS PROGRAM WAS CODED BY BJARKE VIKS0E.
  8.   YOU ARE FREE TO DO WHATEVER YOU WANT WITH THIS PIECE OF CODE.
  9.   E-MAIL ME AT: dat92230@rix02.lyngbyes.dk IN 1994 FOR CHAT AND CODE.
  10.  
  11.     Employs a really basic scheme. Simply store boxes in buffers and
  12.     take one line from each buffer. Eg line 1 from buffer 1, line 2 from
  13.     buffer 2 and so on...
  14. }
  15.  
  16. {$IFNDEF DPMI}
  17.     - must run in protected mode to have enough memory...
  18.     Please enable dpmi-mode
  19. {$ENDIF}
  20.  
  21. uses
  22.     DEMOINIT;
  23.  
  24. const
  25.     DEBUG = FALSE;
  26.     ANTAL_FACES = 6;
  27.     ANTAL_COORDS = 8;
  28.  
  29.     box = 89;
  30.     ANIMWIDTH = 40;
  31.     ANIMHEIGHT = 100;
  32.     ANTAL_ANIMS = ANIMHEIGHT;
  33.  
  34. type
  35.     pAnim = ^animtype;
  36.     animtype = array[0..ANIMWIDTH*ANIMHEIGHT*4] of byte;
  37.  
  38.     facetype = RECORD
  39.         l1,l2,l3,l4 : byte;
  40.     end;
  41.  
  42. var
  43.     slope                    : array[0..399] of integer;
  44.     face                    : array[1..ANTAL_FACES] of facetype;
  45.     light                    : array[1..ANTAL_FACES] of byte;
  46.     cbuffer                : array[0..ANTAL_COORDS*2-1] of integer;
  47.     miny,maxy             : integer;
  48.  
  49.     i : integer;
  50.     xkoord,ykoord,zkoord : integer;
  51.  
  52.     sinustabel            : array[0..1279] of integer;
  53.     v1,v2,v3                : word;
  54.     cos1,sin1,cos2,sin2,cos3,sin3 : integer;
  55.  
  56.     animpos : integer;
  57.     anim : array[0..ANTAL_ANIMS] of pAnim;
  58.     animytabel : array[0..200] of word;
  59.  
  60.  
  61. const
  62.     display1 : integer = $0000;
  63.     display2 : integer = $4000;
  64.     coords : array[0..ANTAL_COORDS*3-1] of integer =
  65.         (box,box,-box, -box,box,-box, -box,-box,-box, box,-box,-box,
  66.         box,box,box, -box,box,box, -box,-box,box, box,-box,box);
  67.  
  68.  
  69. (*------------------------------------------------*)
  70.  
  71. procedure SetupSinus;
  72. var
  73.     i : integer;
  74.     v, vadd : real;
  75. begin
  76.     v:=0.0;
  77.     vadd:=(2.0*pi/1024.0);
  78.     for i:=0 to 1279 do begin
  79.         sinustabel[i]:=round(sin(v)*32767);
  80.         v:=v+vadd;
  81.     end;
  82. end;
  83.  
  84. procedure SetupCoords;
  85. begin
  86.     with face[1] do begin l1:=3; l2:=2; l3:=1; l4:=0; end;
  87.     with face[2] do begin l1:=4; l2:=5; l3:=6; l4:=7; end;
  88.     with face[3] do begin l1:=0; l2:=1; l3:=5; l4:=4; end;
  89.     with face[4] do begin l1:=1; l2:=2; l3:=6; l4:=5; end;
  90.     with face[5] do begin l1:=2; l2:=3; l3:=7; l4:=6; end;
  91.     with face[6] do begin l1:=3; l2:=0; l3:=4; l4:=7; end;
  92. end;
  93.  
  94. procedure SetupColors;
  95. var
  96.     i : integer;
  97. begin
  98.     for i:=0 to 63 do setRGB(i, i,i,i);
  99.     setRGB(0, 2,4,8);
  100. end;
  101.  
  102. procedure InitDemo;
  103. var
  104.     i : integer;
  105. begin
  106.     ClearWholeScreen;
  107.  
  108.     SetupSinus;
  109.     SetupColors;
  110.     SetupCoords;
  111.  
  112.     for i:=0 to ANTAL_ANIMS do begin
  113.         new(anim[i]);
  114.         fillchar(anim[i]^,ANIMWIDTH*ANIMHEIGHT*4,0);
  115.     end;
  116.     for i:=0 to 200 do animytabel[i]:=i*ANIMWIDTH;
  117.  
  118.     v1:=0; v2:=0; v3:=0;
  119.     animpos:=0;
  120. end;
  121.  
  122. procedure UnInitDemo;
  123. var
  124.     i : integer;
  125. begin
  126.     for i:=0 to ANTAL_ANIMS do dispose(anim[i]);
  127. end;
  128.  
  129.  
  130. (*------------------------------------------------*)
  131.  
  132. procedure SwapDisplay;
  133. var
  134.     temp : word;
  135. begin
  136.     temp:=display2;
  137.     display2:=display1;
  138.     display1:=temp;
  139.     SetAddress(Ptr(SEGA000,display1));
  140. end;
  141.  
  142. procedure ClearScreen(anim : pAnim); assembler;
  143. asm
  144.     les    di,anim
  145.     DB $66,$33,$c0        {xor eax,eax}
  146.     mov    cx,ANIMWIDTH*ANIMHEIGHT
  147.     cld
  148.     DB $F3,$66,$AB        {rep stosd}
  149. end;
  150.  
  151.  
  152. (*------------------------------------------------*)
  153.  
  154. procedure ClearSlope; assembler;
  155. asm
  156.     mov    ax,ds
  157.     mov    es,ax
  158.     lea    di,slope
  159.     DB $66,$B8,$00,$80,$00,$80        {MOV AX,$80008000}
  160.     cld
  161.     mov    cx,200
  162.     DB $F3,$66,$AB                        {REP STOSD}
  163. end;
  164.  
  165. procedure CalcSlope(l1,l2 : integer); assembler;
  166. var
  167.     ysize : integer;
  168. asm
  169.     lea    si,cbuffer
  170.     mov    bx,l1
  171.     shl    bx,2
  172.     mov    cx,[si+bx]
  173.     mov    dx,[si+bx+2]
  174.     mov    bx,l2
  175.     shl    bx,2
  176.     add    si,bx
  177.     mov    ax,[si]
  178.     mov    bx,[si+2]
  179.  
  180.     cmp    bx,dx
  181.     jle    @noswap
  182.     xchg    ax,cx
  183.     xchg    bx,dx
  184. @noswap:
  185.     cmp    bx,miny
  186.     jae    @miny
  187.     mov    miny,bx
  188. @miny:
  189.     cmp    dx,maxy
  190.     jbe    @maxy
  191.     mov    maxy,dx
  192. @maxy:
  193.  
  194.     sub    dx,bx
  195.     mov    ysize,dx
  196.     shl    bx,2
  197.     lea    si,slope
  198.     add    si,bx
  199.  
  200.     push    ax
  201.     sub    cx,ax
  202.     inc    cx
  203.  
  204.     and    dx,dx
  205.     jz        @zero
  206.     cmp    dl,1
  207.     jne    @not1
  208.     dec    cx
  209.     mov    dx,cx
  210.     xor    ax,ax
  211.     jmp    @one
  212. @not1:
  213.     cmp    dl,2
  214.     jne    @not2
  215.     mov    ax,$7FFF
  216.     imul    cx
  217.     jmp    @one
  218. @not2:
  219.  
  220.     mov    dx,$0001
  221.     mov    ax,$0000
  222.     idiv    ysize
  223.     imul    cx
  224. @one:
  225.     pop    cx
  226.     xor    bx,bx
  227.  
  228.     mov    di,$8000
  229. @loop:
  230.     cmp    [si],di
  231.     jne    @other
  232.     mov    [si],cx
  233.     add    si,4
  234.     add    bx,ax
  235.     adc    cx,dx
  236.     dec    ysize
  237.     jnz    @loop
  238.     jmp    @zero
  239. @other:
  240.     mov    [si+2],cx
  241.     add    si,4
  242.     add    bx,ax
  243.     adc    cx,dx
  244.     dec    ysize
  245.     jnz    @loop
  246. @zero:
  247. end;
  248.  
  249.  
  250. (*------------------------------------------------*)
  251.  
  252. procedure CalcVinkel;
  253. begin
  254.     sin1:=sinustabel[v1];
  255.     cos1:=sinustabel[v1+256];
  256.     sin2:=sinustabel[v2];
  257.     cos2:=sinustabel[v2+256];
  258.     sin3:=sinustabel[v3];
  259.     cos3:=sinustabel[v3+256];
  260.  
  261.     v1:=(v1+2) AND 1023;
  262.     v2:=(v2-2) AND 1023;
  263.     v3:=(v3+1) AND 1023;
  264. end;
  265.  
  266. procedure RotateAllCoords; assembler;
  267. asm
  268.     mov    ax,ds
  269.     mov    es,ax
  270.     lea    si,coords
  271.     lea    di,cbuffer
  272.     mov    i,ANTAL_COORDS
  273.     cld
  274. @loop:
  275.     lodsw
  276.     mov    xkoord,ax
  277.     lodsw
  278.     mov    ykoord,ax
  279.     lodsw
  280.     mov    zkoord,ax
  281.  
  282.     mov    ax,xkoord               {rotate around Z-axis}
  283.     push    ax
  284.     imul    Cos1
  285.     add    ax,ax
  286.     adc    dx,dx
  287.     mov    bx,dx
  288.     mov    ax,ykoord
  289.     imul    Sin1
  290.     add    ax,ax
  291.     adc    dx,dx
  292.     sub    bx,dx
  293.     mov    xkoord,bx
  294.     pop    ax
  295.     imul    Sin1
  296.     add    ax,ax
  297.     adc    dx,dx
  298.     mov    bx,dx
  299.     mov    ax,ykoord
  300.     imul    Cos1
  301.     add    ax,ax
  302.     adc    dx,dx
  303.     add    bx,dx
  304.     mov    ykoord,bx
  305.  
  306.     mov    ax,ykoord               {rotate around Y-axis}
  307.     push    ax
  308.     imul    Cos2
  309.     add    ax,ax
  310.     adc    dx,dx
  311.     mov    bx,dx
  312.     mov    ax,zkoord
  313.     imul    Sin2
  314.     add    ax,ax
  315.     adc    dx,dx
  316.     sub    bx,dx
  317.     mov    ykoord,bx
  318.     pop    ax
  319.     imul    Sin2
  320.     add    ax,ax
  321.     adc    dx,dx
  322.     mov    bx,dx
  323.     mov    ax,zkoord
  324.     imul    Cos2
  325.     add    ax,ax
  326.     adc    dx,dx
  327.     add    bx,dx
  328.     mov    zkoord,bx
  329.  
  330.     mov    ax,xkoord               {rotate around X-axis}
  331.     push    ax
  332.     imul    Cos3
  333.     add    ax,ax
  334.     adc    dx,dx
  335.     mov    bx,dx
  336.     mov    ax,zkoord
  337.     imul    Sin3
  338.     add    ax,ax
  339.     adc    dx,dx
  340.     sub   bx,dx
  341.     mov    xkoord,bx
  342.     pop    ax
  343.     imul    Sin3
  344.     add    ax,ax
  345.     adc    dx,dx
  346.     mov    bx,dx
  347.     mov    ax,zkoord
  348.     imul    Cos3
  349.     add    ax,ax
  350.     adc    dx,dx
  351.     add    bx,dx
  352.     mov    zkoord,bx
  353.  
  354.     add    bx,800
  355.     and    bx,bx
  356.     jnz    @zero
  357.     mov    bl,1
  358. @zero:
  359.  
  360.     mov        ax,xkoord
  361.     cwd
  362.     mov        dl,ah
  363.     mov        ah,al
  364.     xor        al,al
  365.     idiv        bx
  366.     add        ax,80
  367.     stosw
  368.  
  369.     mov        ax,ykoord
  370.     cwd
  371.     mov        dl,ah
  372.     mov        ah,al
  373.     xor        al,al
  374.     idiv        bx
  375.     add        ax,50
  376.     stosw
  377.  
  378.     dec        i
  379.     jne        @loop
  380. end;
  381.  
  382.  
  383. function FaceShown(i : integer; l1,l2,l3 : byte) : boolean;
  384. var
  385.     a,b : longint;
  386. begin
  387.     a := (cbuffer[l1]-cbuffer[l2])*(cbuffer[l3+1]-cbuffer[l2+1]);
  388.     b := (cbuffer[l1+1]-cbuffer[l2+1])*(cbuffer[l3]-cbuffer[l2]);
  389.     light[i] := ((a-b) DIV 70)+1;
  390.     FaceShown := (a-b) > 0;
  391. end;
  392.  
  393.  
  394. procedure FillShape(anim : pAnim; y,ysize : integer; color : byte); assembler;
  395. const
  396.     PSIZE = ANIMWIDTH*ANIMHEIGHT;
  397.     planeadd : array[0..3] of word = (0,PSIZE,PSIZE*2,PSIZE*3);
  398. asm
  399.     mov    ax,y
  400.     shl    ax,1
  401.     mov    si,ax
  402.     les    di,anim
  403.     add    di,[si+OFFSET animytabel]
  404.     lea    si,slope
  405.     shl    ax,1
  406.     add    si,ax
  407.  
  408.     cld
  409. @yloop:
  410.     lodsw
  411.     mov    dx,ax
  412.     lodsw
  413.     cmp    ax,dx
  414.     jle    @exchange
  415.     xchg    ax,dx
  416. @exchange:
  417.     push    di
  418.  
  419.     mov    bx,ax
  420.     sub    dx,ax            {calc xsize in DX}
  421.     cmp    dx,0
  422.     jle    @drawn
  423.     cmp    dx,ANIMWIDTH*4
  424.     jge    @drawn
  425.     shr    ax,2            {calc xpos}
  426.     add    di,ax
  427.  
  428.     and    bx,3
  429.     shl    bl,1
  430.     add    di,WORD PTR [planeadd+bx]
  431.     shr    bl,1
  432.     mov    ah,4
  433.     sub    ah,bl
  434.  
  435.     mov    cx,dx
  436.     mov    dx,ANIMWIDTH*ANIMHEIGHT
  437.     mov    bx,(ANIMWIDTH*ANIMHEIGHT*4)-1
  438.     mov    al,color
  439. @xloop:
  440.     mov    es:[di],al
  441.     add    di,dx
  442.     dec    ah
  443.     jnz    @noswap
  444.     mov    ah,4
  445.     sub    di,bx
  446. @noswap:
  447.     loop    @xloop
  448.  
  449. @drawn:
  450.     pop    di
  451.     add    di,ANIMWIDTH
  452.     dec    ysize
  453.     jnz    @yloop
  454. end;
  455.  
  456.  
  457. procedure PrintJellyLogo;
  458. var
  459.     i,pos : integer;
  460.     aptr : pAnim;
  461.     source_offset, dest_offset : word;
  462.     colorptr : pointer;
  463. begin
  464.     pos:=animpos;
  465.     source_offset:=0;                                    {start with 1. line...}
  466.     dest_offset:=20+(50*WIDTH)+display1;        {start pos on screen}
  467.     for i:=0 to ANIMHEIGHT-1 do begin
  468.         aptr:=@anim[pos]^;
  469.         asm
  470.             push    ds
  471.             cli
  472.             mov    dx,$3C4
  473.             mov    al,$02
  474.             out    dx,al
  475.             inc    dx
  476.             mov    al,$01
  477.             out    dx,al
  478.             sti
  479.             mov    es,SEGA000
  480.             mov    di,dest_offset
  481.             lds    si,aptr
  482.             add    si,source_offset
  483.             cld
  484.             mov    cx,ANIMWIDTH/2
  485.             rep movsw
  486.  
  487.             cli
  488.             mov    dx,$3C4
  489.             mov    al,$02
  490.             out    dx,al
  491.             inc    dx
  492.             mov    al,$02
  493.             out    dx,al
  494.             sti
  495.             mov    di,dest_offset
  496.             add    si,(ANIMWIDTH*ANIMHEIGHT)-ANIMWIDTH
  497.             mov    cx,ANIMWIDTH/2
  498.             rep movsw
  499.  
  500.             cli
  501.             mov    dx,$3C4
  502.             mov    al,$02
  503.             out    dx,al
  504.             inc    dx
  505.             mov    al,$04
  506.             out    dx,al
  507.             sti
  508.             mov    di,dest_offset
  509.             add    si,(ANIMWIDTH*ANIMHEIGHT)-ANIMWIDTH
  510.             mov    cx,ANIMWIDTH/2
  511.             rep movsw
  512.  
  513.             cli
  514.             mov    dx,$3C4
  515.             mov    al,$02
  516.             out    dx,al
  517.             inc    dx
  518.             mov    al,$08
  519.             out    dx,al
  520.             sti
  521.             mov    di,dest_offset
  522.             add    si,(ANIMWIDTH*ANIMHEIGHT)-ANIMWIDTH
  523.             mov    cx,ANIMWIDTH/2
  524.             rep movsw
  525.             pop    ds
  526.         end;
  527.         inc(source_offset,ANIMWIDTH);
  528.         inc(dest_offset,WIDTH);
  529.         inc(pos); if (pos > ANTAL_ANIMS) then pos:=0;
  530.     end;
  531. end;
  532.  
  533.  
  534.  
  535. procedure RunOnce;
  536. var
  537.     i : integer;
  538. begin
  539.     SwapDisplay;
  540.     VBLANK_QUICK;
  541.     if DEBUG then SetRGB(0,30,0,0);
  542.  
  543.     ClearScreen(anim[animpos]);
  544.  
  545.     CalcVinkel;
  546.     RotateAllCoords;
  547.  
  548.     for i:=1 to ANTAL_FACES do begin
  549.         with face[i] do if FaceShown(i, l1 shl 1,l2 shl 1,l3 shl 1) then begin
  550.             ClearSlope;
  551.             miny := 200; maxy := 0;
  552.             CalcSlope(l1,l2);
  553.             CalcSlope(l2,l3);
  554.             CalcSlope(l3,l4);
  555.             CalcSlope(l4,l1);
  556.             FillShape(anim[animpos], miny, maxy-miny, light[i]);
  557.         end;
  558.     end;
  559.     PrintJellyLogo;
  560.     inc(animpos); if (animpos > ANTAL_ANIMS) then animpos:=0;
  561.     if DEBUG then SetRGB(0,0,0,0);
  562. end;
  563.  
  564.  
  565. begin
  566.     OpenScreen;
  567.     InitDemo;
  568.     repeat RunOnce until KeyPressed;
  569.     UninitDemo;
  570.     CloseScreen;
  571. end.
  572.